<template>
  <el-dialog
    :title="!dataForm.oldHouseid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地区id" prop="mapid">
      <el-input v-model="dataForm.mapid" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="二手房价格" prop="oldHouseprice">
      <el-input v-model="dataForm.oldHouseprice" placeholder="二手房价格"></el-input>
    </el-form-item>
    <el-form-item label="二手房面积" prop="oldHousearea">
      <el-input v-model="dataForm.oldHousearea" placeholder="二手房面积"></el-input>
    </el-form-item>
    <el-form-item label="二手房户型(1.一室 2.二室 3.三室)" prop="oldHouseroom">
      <el-input v-model="dataForm.oldHouseroom" placeholder="二手房户型(1.一室 2.二室 3.三室)"></el-input>
    </el-form-item>
    <el-form-item label="二手房类型(1.住宅 2.别墅 3.商业 4.写字楼))" prop="oldHousetype">
      <el-input v-model="dataForm.oldHousetype" placeholder="二手房类型(1.住宅 2.别墅 3.商业 4.写字楼))"></el-input>
    </el-form-item>
    <el-form-item label="二手房装修(1.精装修 2.普通装修 3.毛坯房)" prop="oldHousedeco">
      <el-input v-model="dataForm.oldHousedeco" placeholder="二手房装修(1.精装修 2.普通装修 3.毛坯房)"></el-input>
    </el-form-item>
    <el-form-item label="二手房特色(1.近地铁 2.环境好 3.低价)" prop="oldHousefeat">
      <el-input v-model="dataForm.oldHousefeat" placeholder="二手房特色(1.近地铁 2.环境好 3.低价)"></el-input>
    </el-form-item>
    <el-form-item label="二手房图片" prop="oldHouseimg">
      <el-input v-model="dataForm.oldHouseimg" placeholder="二手房图片"></el-input>
    </el-form-item>
    <el-form-item label="二手房简介" prop="oldHousesyno">
      <el-input v-model="dataForm.oldHousesyno" placeholder="二手房简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          oldHouseid: 0,
          mapid: '',
          oldHouseprice: '',
          oldHousearea: '',
          oldHouseroom: '',
          oldHousetype: '',
          oldHousedeco: '',
          oldHousefeat: '',
          oldHouseimg: '',
          oldHousesyno: ''
        },
        dataRule: {
          mapid: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          oldHouseprice: [
            { required: true, message: '二手房价格不能为空', trigger: 'blur' }
          ],
          oldHousearea: [
            { required: true, message: '二手房面积不能为空', trigger: 'blur' }
          ],
          oldHouseroom: [
            { required: true, message: '二手房户型(1.一室 2.二室 3.三室)不能为空', trigger: 'blur' }
          ],
          oldHousetype: [
            { required: true, message: '二手房类型(1.住宅 2.别墅 3.商业 4.写字楼))不能为空', trigger: 'blur' }
          ],
          oldHousedeco: [
            { required: true, message: '二手房装修(1.精装修 2.普通装修 3.毛坯房)不能为空', trigger: 'blur' }
          ],
          oldHousefeat: [
            { required: true, message: '二手房特色(1.近地铁 2.环境好 3.低价)不能为空', trigger: 'blur' }
          ],
          oldHouseimg: [
            { required: true, message: '二手房图片不能为空', trigger: 'blur' }
          ],
          oldHousesyno: [
            { required: true, message: '二手房简介不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.oldHouseid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.oldHouseid) {
            this.$http({
              url: this.$http.adornUrl(`/house/oldhouse/info/${this.dataForm.oldHouseid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mapid = data.oldhouse.mapid
                this.dataForm.oldHouseprice = data.oldhouse.oldHouseprice
                this.dataForm.oldHousearea = data.oldhouse.oldHousearea
                this.dataForm.oldHouseroom = data.oldhouse.oldHouseroom
                this.dataForm.oldHousetype = data.oldhouse.oldHousetype
                this.dataForm.oldHousedeco = data.oldhouse.oldHousedeco
                this.dataForm.oldHousefeat = data.oldhouse.oldHousefeat
                this.dataForm.oldHouseimg = data.oldhouse.oldHouseimg
                this.dataForm.oldHousesyno = data.oldhouse.oldHousesyno
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/oldhouse/${!this.dataForm.oldHouseid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'oldHouseid': this.dataForm.oldHouseid || undefined,
                'mapid': this.dataForm.mapid,
                'oldHouseprice': this.dataForm.oldHouseprice,
                'oldHousearea': this.dataForm.oldHousearea,
                'oldHouseroom': this.dataForm.oldHouseroom,
                'oldHousetype': this.dataForm.oldHousetype,
                'oldHousedeco': this.dataForm.oldHousedeco,
                'oldHousefeat': this.dataForm.oldHousefeat,
                'oldHouseimg': this.dataForm.oldHouseimg,
                'oldHousesyno': this.dataForm.oldHousesyno
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
